package com.woniu.supermarket.web;


import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Brand;
import com.woniu.supermarket.entity.Department;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("Department")
@Api(value = "部门操作")
public class DepartmentController {

    @Autowired
    IDepartmentService iDepartmentService;

    @GetMapping("/showDepartment")
    @ApiOperation(value = "部门分页查询")
    public ResultResponse<PageInfo<Department>> showDepartment(@ApiParam(name = "now",value = "当前页") @RequestParam(name = "now",defaultValue = "1") Integer now,
                                                               @ApiParam(name = "size",value = "页大小") @RequestParam(name = "size",defaultValue = "3") Integer size,
                                                               @ApiParam(name = "name",value = "部门名称") String name){

        ResultResponse<PageInfo<Department>> result = null;
        try {
            PageInfo<Department> pageInfo = iDepartmentService.findByConds(now, size, name);
            result = new ResultResponse<>(200,"查询部门成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询部门失败");
        }
        return result;
    }
    @GetMapping("/dept")
    @ApiOperation(value = "部门查询")
        public ResultResponse<List<Department>> AllList(){
        ResultResponse<List<Department>> result = null;
        try {
            List<Department> lists = iDepartmentService.findAll();
            result = new ResultResponse<>(200,"查询所有部门成功",lists);
        }catch (RuntimeException e){
            result = new ResultResponse<>(201,"查询所有部门失败");
        }
        return result;
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "部门删除")
    public ResultResponse<Void> del(@PathVariable("id") Integer id){

        ResultResponse<Void> result = null;
        try {
            int i = iDepartmentService.removeDepartment(id);
            result = new ResultResponse<>(200,"删除部门成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除部门失败");
        }
        return result;
    }


    @PutMapping("/modifyDepartment")
    @ApiOperation(value = "部门修改")
    public ResultResponse<Void> modifyDepartment(@RequestBody Department department){

        ResultResponse<Void> result = null;
        try {
            int i = iDepartmentService.modifyDepartment(department);
            result = new ResultResponse<>(200,"修改部门成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改部门失败");
        }
        return result;
    }



    @PostMapping("/addDepartment")
    @ApiOperation(value = "部门添加")
    public ResultResponse<Void> addDepartment(@RequestBody Department department){

        ResultResponse<Void> result = null;
        try {
            int i = iDepartmentService.addDepartment(department);
            result = new ResultResponse<>(200,"添加部门成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加部门失败");
        }
        return result;
    }

}
